package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.dto.PageDto;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.ResponseUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 11:12
 */
@RestController
@RequestMapping("/admin/coupon")
@Slf4j
@CrossOrigin
public class CouponController {
    @Autowired
    private DtsCouponService dtsCouponService;
    @Autowired
    private DtsCouponUserService dtsCouponUserService;
    //http://localhost:8080/admin/coupon/list?page=1&limit=20&name=dd&type=0&status=0&sort=add_time&order=desc

    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="查找")
    @RequiresPermissions("admin:coupon:list")
    @GetMapping("/list")
    public  Object couponList(PageDto pageDto){
           Page<DtsCoupon> couponPage= dtsCouponService.selectPage(pageDto);
        List<DtsCoupon> records = couponPage.getRecords();
        long total = couponPage.getTotal();

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);

    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="详情")
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("/read")
    public  Object readCoupon(Integer id){

       DtsCoupon dtsCoupon= dtsCouponService.selectById(id);
       return ResponseUtil.ok(dtsCoupon);
    }
//http://localhost:8080/admin/coupon/listuser?page=1&limit=20&couponId=11&userId=ffff&status=1&sort=add_time&order=desc

 @GetMapping("/listuser")
    public  Object userList(PageDto pageDto){
       Page<DtsCouponUser> dtsCouponUserPage=  dtsCouponUserService.selectPage(pageDto);
     long total = dtsCouponUserPage.getTotal();
     List<DtsCouponUser> records = dtsCouponUserPage.getRecords();
     HashMap<String, Object> data = new HashMap<>();
     data.put("total", total);
     data.put("items", records);
     return ResponseUtil.ok(data);
 }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="编辑")
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("/update")
    public  Object updateCoupon(@RequestBody DtsCoupon dtsCoupon){
        if(dtsCoupon==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponService.update(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="添加")
    @RequiresPermissions("admin:coupon:create")
    @PostMapping("/create")
    public  Object createCoupon(@RequestBody DtsCoupon dtsCoupon){
        if(dtsCoupon==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsCoupon.setUpdateTime(new Date());
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setDeleted(false);
        dtsCouponService.insert(dtsCoupon);
        return ResponseUtil.ok(dtsCoupon);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button ="删除")
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("/delete")
    public  Object deleteCoupon(@RequestBody DtsCoupon dtsCoupon){
        if(dtsCoupon==null){
            return  ResponseUtil.badArgumentValue();
        }
        dtsCoupon.setUpdateTime(new Date());
        dtsCoupon.setDeleted(true);
        dtsCouponService.delete(dtsCoupon);
        return ResponseUtil.ok();}
}
